﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using octopus_project.scene;
using octopus_project.input;
using Microsoft.Xna.Framework;
using octopus_project.tileset;
using octopus_project.screen;
using octopus_project.typegame;
using System;
using octopus_project.scene.cannon.weapon;

namespace octopus_project.scene.war {
    class War_Gameplay : Gameplay_GameScreen {
        Dictionary<PlayerIndex, War_Cursor> cursors;
        Dictionary<PlayerIndex, StatusBar_GameplayEntry> status;

        private TypeGame typeGame;
        
        byte cursorCurrentSensitivity;
        byte cursorLowSensitivity = 3;
        byte cursorHighSensitivity = 6;

        public War_Gameplay(ScreenManager screenManager, TypeGame paramTypeGame)
            : base(screenManager) {

            typeGame = paramTypeGame;
            TransitionOffTime = TimeSpan.Zero;
        }

        public void createAll() {
            cursorCurrentSensitivity = cursorLowSensitivity;
            cursors = new Dictionary<PlayerIndex, War_Cursor>();
            status = new Dictionary<PlayerIndex, StatusBar_GameplayEntry>();

            War_Cursor cursor1 = new War_Cursor(this, Game1.Players[PlayerIndex.One]);
            cursor1.PositionX = 300;
            cursor1.PositionY = 150;
            cursors.Add(PlayerIndex.One, cursor1);
            status.Add(PlayerIndex.One, new StatusBar_GameplayEntry(this, Game1.Players[PlayerIndex.One]));

            War_Cursor cursor2 = new War_Cursor(this, Game1.Players[PlayerIndex.Two]);
            cursor2.PositionX = 1300;
            cursor2.PositionY = 150;
            cursors.Add(PlayerIndex.Two, cursor2);
            status.Add(PlayerIndex.Two, new StatusBar_GameplayEntry(this, Game1.Players[PlayerIndex.Two]));

            /*War_Cursor cursor3 = new War_Cursor(this, Game1.Players[PlayerIndex.Three]);
            cursor3.PositionX = 300;
            cursor3.PositionY = 750;
            cursors.Add(PlayerIndex.Three, cursor3);
            status.Add(PlayerIndex.Three, new StatusBar_GameplayEntry(this, Game1.Players[PlayerIndex.Three]));

            War_Cursor cursor4 = new War_Cursor(this, Game1.Players[PlayerIndex.Four]);
            cursor4.PositionX = 1300;
            cursor4.PositionY = 750;
            cursors.Add(PlayerIndex.Four, cursor4);
            status.Add(PlayerIndex.Four, new StatusBar_GameplayEntry(this, Game1.Players[PlayerIndex.Four]));
            */
            //Game1.battlegroundMap = new TileMap_DC(this, "../../../../octopus-projectContent/map/Map2.csv");
            //timer = new Timer_DC(Game);

            //GameplayEntries.Add(inputHandler);
            GameplayEntries.Add(Game1.battlegroundMap);
            GameplayEntries.Add(typeGame.Timer);

            GameplayEntries.AddRange(status.Values);
            GameplayEntries.AddRange(cursors.Values);
        }


        /// <summary>
        /// Lets the game respond to player input. Unlike the Update method,
        /// this will only be called when the gameplay screen is active.
        /// </summary>
        public override void HandleInput (InputHandler input) {
            base.HandleInput(input);

            foreach(Player aPlayer in Game1.Players.Values) {
                if(input.IsKeyDown(aPlayer.getActionKey(ActionKey.DOWN)) || (aPlayer.getActionButton(ActionKey.DOWN) != 0 && input.IsButtonDown(aPlayer.getActionButton(ActionKey.DOWN)))) {
                    if(cursors[aPlayer.PlayerIndex].PositionY < (Constant.HEIGTH_RESOLUTION - 1)) {
                        cursors[aPlayer.PlayerIndex].PositionY += cursorCurrentSensitivity;
                    }
                }
                if(input.IsKeyDown(aPlayer.getActionKey(ActionKey.UP)) || (aPlayer.getActionButton(ActionKey.UP) != 0 && input.IsButtonDown(aPlayer.getActionButton(ActionKey.UP)))) {
                    if(cursors[aPlayer.PlayerIndex].PositionY > cursorCurrentSensitivity) {
                        cursors[aPlayer.PlayerIndex].PositionY -= cursorCurrentSensitivity;
                    }
                }
                if(input.IsKeyDown(aPlayer.getActionKey(ActionKey.RIGHT)) || (aPlayer.getActionButton(ActionKey.RIGHT) != 0 && input.IsButtonDown(aPlayer.getActionButton(ActionKey.RIGHT)))) {
                    if(cursors[aPlayer.PlayerIndex].PositionX < (Constant.WIDTH_RESOLUTION - 1)) {
                        cursors[aPlayer.PlayerIndex].PositionX += cursorCurrentSensitivity;
                    }
                }
                if(input.IsKeyDown(aPlayer.getActionKey(ActionKey.LEFT)) || (aPlayer.getActionButton(ActionKey.LEFT) != 0 && input.IsButtonDown(aPlayer.getActionButton(ActionKey.LEFT)))) {
                    if(cursors[aPlayer.PlayerIndex].PositionX > cursorCurrentSensitivity) {
                        cursors[aPlayer.PlayerIndex].PositionX -= cursorCurrentSensitivity;
                    }
                }
                if(input.IsNewKeyPress(aPlayer.getActionKey(ActionKey.FIRE)) || (aPlayer.getActionButton(ActionKey.FIRE) != 0 && input.IsButtonDown(aPlayer.getActionButton(ActionKey.FIRE)))) {

                    Damage damage = aPlayer.Stats.shotting();
                    if (damage != null) {

                        MapCell mapCell = Game1.battlegroundMap.getMapCell(cursors[aPlayer.PlayerIndex].cursorPositionX(), cursors[aPlayer.PlayerIndex].cursorPositionY());
                        //Game1.battlegroundMap.shootCannon(aPlayer, cursors[aPlayer.PlayerIndex].cursorPositionX(), cursors[aPlayer.PlayerIndex].cursorPositionY());
                        mapCell.shootMe(damage);
                    }
                    
                }
                if(input.IsKeyDown(aPlayer.getActionKey(ActionKey.ROTATE)) || (aPlayer.getActionButton(ActionKey.ROTATE) != 0 && input.IsButtonDown(aPlayer.getActionButton(ActionKey.ROTATE)))) {
                    if(cursorCurrentSensitivity == cursorLowSensitivity) {
                        cursorCurrentSensitivity = cursorHighSensitivity;
                    }
                    else {
                        cursorCurrentSensitivity = cursorLowSensitivity;
                    }

                }
            }

        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen) {

            if (!typeGame.Timer.isComplete) {
                base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
            }
            else {
                typeGame.nextPhase();
            }

        }
    }
}
